// src/api/CustomerApi.ts
import { BaseApiClient } from '../BaseApi.ts';
import type { CustomerData, ApiResponse } from '../../types/DataTypes.ts';

// 客户相关API
export class CustomerApi extends BaseApiClient {
    constructor() {
        super();
    }

    // 获取所有客户
    async getAllCustomers(): Promise<ApiResponse<CustomerData[]>> {
        return this.request<CustomerData[]>({
            method: 'GET',
            url: '/customers',
        });
    }

    // 搜索客户（按客户名）
    async searchCustomers(customerName: string): Promise<ApiResponse<CustomerData[]>> {
        const params = new URLSearchParams();
        params.append('customerName', customerName);

        return this.request<CustomerData[]>({
            method: 'GET',
            url: `/customers/search?${params.toString()}`,
        });
    }

    // 检查用户名是否存在
    async checkCustomerNameExists(customerName: string): Promise<ApiResponse<{ exists: boolean }>> {
        return this.request<{ exists: boolean }>({
            method: 'GET',
            url: `/customers/exists/${encodeURIComponent(customerName)}`,
        });
    }

    // 用户登录验证
    async verifyLogin(customerName: string, customerId: string): Promise<ApiResponse<{ customerName: string; customerId: string }>> {
        return this.request<{ customerName: string; customerId: string }>({
            method: 'POST',
            url: '/customers/verify',
            data: {
                customerName,
                customerId
            },
        });
    }

    // 添加新客户
    async addCustomer(customer: CustomerData): Promise<ApiResponse<CustomerData>> {
        return this.request<CustomerData>({
            method: 'POST',
            url: '/customers',
            data: customer,
        });
    }

    // 更新客户信息
    async updateCustomer(customerData: CustomerData): Promise<ApiResponse> {
        return this.request({
            method: 'PUT',
            url: '/customers/update',
            data: customerData,
        });
    }

    // 删除客户
    async deleteCustomer(customerId: string): Promise<ApiResponse> {
        return this.request({
            method: 'DELETE',
            url: `/customers/${encodeURIComponent(customerId)}`,
        });
    }
}

// 创建单例实例
export const customerApi = new CustomerApi();

// 默认导出
export default customerApi;